var app = getApp();
import config from "../../config.js";
const base = config;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    logo: '',
    name: '',
    url: app.globalData.url,
    is_login: true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.initScene();
  },
  initScene: function() {
    var that = this;
    wx.login({
      success: function(res) {
        wx.request({
          url: base.CheckOpenId,
          data: {
            code: res.code
          },
          success: function(res) {
            console.log("openId" + res.data.data.openid);
            wx.setStorageSync('openID', res.data.data.openid);
            that.login();
          }
        })
      }
    })

  },
  login: function() {
    var that = this;
    //自动登录
    //判断是否登录
    var openID = wx.getStorageSync('openID'),
      session = wx.getStorageSync('session');
    console.log("session" + session);
    if (openID != null && session != null) {
      wx.request({
        url: base.isLogin,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': session
        },
        success: function(res) {
          console.log(res);
          //已登录
          if (res.data.status == 200) {
            app.globalData.getUserInfo();
          } else {
            //未登录
            wx.request({
              url: base.autoLogin,
              header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': session
              },
              data: {
                openId: openID
              },
              success: function(res) {
                console.log(res);
                wx.hideLoading();

                if (res.data.status == 200) {
                  setTimeout(function() {
                    app.globalData.getUserInfo();
                  }, 1000);
                } else {
                  wx.reLaunch({
                    url: '../login/login',
                  })

                }
              }
            })

          }
        }
      })
    } else {
      wx.reLaunch({
        url: '../login/login',
      })
    }
  },
})